const Router = require('koa-router');
const router = new Router({
    prefix: '/v1/book'
});
const {
    HotBook
} = require('../../models/hot-book')

const {
    PositiveIntegerValidator, SearchValidator, AddShortCommentValidator
} = require('../../validators/validator')

const {
    Book
} = require('../../models/book')

const {
    Auth
} = require('../../../middlewares/auth')

const {
    Comment
} = require('../../models/book_comment')

const success = require('../../lib/helper')

// 获取热门书籍点赞数的接口
router.get('/hot_list', async (ctx, next) => {
    const favors = await HotBook.getAll();
    ctx.body = favors;
});
  
// 获取书籍的详细信息
router.get('/:id/detail', async (ctx, next) => {
    const v = await new PositiveIntegerValidator().validate(ctx);
    const book = new Book();
    const bookValue = await book.detail(v.get('path.id'));
    ctx.body = bookValue.data;
})

// 获取热门书籍的接口
router.get('/search', async (ctx, next) => {
    const v = await new SearchValidator().validate(ctx);
    const result = await Book.searchFromYuShu(v.get('query.q'), v.get('query.count'), v.get('query.start'));
    ctx.body = result.books;
})

// 获取我喜欢的书籍的数量接口实现
router.get('/favor/count', new Auth().m, async (ctx, next) => {
    const count = await Book.getMyFavorBookCount(ctx.auth.uid);
    ctx.body = {
        count
    }
})

// 获取某个书籍的点赞情况
router.get('/:book_id/favor', new Auth().m, async (ctx, next) => {
    const v = await new PositiveIntegerValidator().validate(ctx, {
        id: 'book_id'
    }); 
    const result = await Book.getBookFavor(ctx.auth.uid, v.get('path.book_id'));
    ctx.body = result;
});


// 新增短评的接口的实现
router.post('/add/short_comment', new Auth().m, async (ctx, next) => {
   const v = await new AddShortCommentValidator().validate(ctx, {
       id: 'book_id'
   });
   const result = await Comment.addComment(v.get('body.book_id'), v.get('body.content'));
   success();
});

// 获取到该书籍的全部短评内容
router.get('/:book_id/short_comment', new Auth().m, async (ctx, next) => {
    const v= await new PositiveIntegerValidator().validate(ctx, {
        id: 'book_id'
    });
    const results = await Comment.getComments(v.get('path.book_id'));
    ctx.body = results;
})






module.exports = router;